package alipay.openplatform.edu.campus.jiuye.common.utils;

import com.google.common.base.Strings;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by ZhengGong on 15/3/16.
 * Description 时间操作工具类
 */
public class DateUtil {
    public static Date addYear(Date date, int year) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.YEAR, year);//属性很多也有月等等，可以操作各种时间日期
        return c.getTime();
    }

    public static String formatToSecond(Date date) {
        if (ObjectUtils.isEmpty(date)) {
            return "";
        }
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(date);
    }

    public static String formatToDay(Date date) {
        DateFormat format = new SimpleDateFormat("yyyyMMdd");
        return format.format(date);
    }

    public static String formatToyMdHmsS() {
        Date date = new Date();
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        return format.format(date);
    }

    public static String formatToMinute(Date date) {
        DateFormat format = new SimpleDateFormat("yyyyMMddHHmm");
        return format.format(date);
    }

    public static String formatToMonth(Date date) {
        DateFormat format = new SimpleDateFormat("yyyyMM");
        return format.format(date);
    }

    public static String formatToHour(Date date) {
        DateFormat format = new SimpleDateFormat("yyyyMMddHH");
        return format.format(date);
    }

    public static String formatToYear(Date date) {
        DateFormat format = new SimpleDateFormat("yyyy");
        return format.format(date);
    }

    public static String formatMMdd(Date date) {
        DateFormat df = new SimpleDateFormat("MMdd");
        return df.format(date);
    }

    public static Date stringSecondToDate(String str) {
        if (Strings.isNullOrEmpty(str)) return null;

        DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            return format.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Date stringDateToDate(String str) {
        if (Strings.isNullOrEmpty(str)) return null;

        DateFormat format = new SimpleDateFormat("yyyyMMdd");
        try {
            return format.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Date addDay(Date date, int day) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.DAY_OF_YEAR, day);//属性很多也有月等等，可以操作各种时间日期
        return c.getTime();
    }

    public static Date addMinutes(Date date, int minutes) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.MINUTE, minutes);//属性很多也有月等等，可以操作各种时间日期
        return c.getTime();
    }

    /**
     * 得到昨日日期
     *
     * @param date 日期
     * @return Date
     */
    public static Date getYesterdayForDay(Date date) {
        try {
            return stringDateToDate(formatToDay(addDay(date, -1)));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 比较两个日期之间的大小
     *
     * @param d1
     * @param d2
     * @return 前者大于后者返回true 反之false
     */
    public static boolean compareDate(Date d1, Date d2) {
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
        c1.setTime(d1);
        c2.setTime(d2);

        int result = c1.compareTo(c2);

        return result >= 0;
    }

    /**
     * 日期 加减 n个月
     *
     * @param date
     * @param n
     * @return
     */
    public static Date changeMonth(Date date, int n) {
        Calendar cl = Calendar.getInstance();
        cl.setTime(date);
        cl.add(Calendar.MONTH, n);
        date = cl.getTime();
        return date;
    }

    /**
     * 计算两个日期之间相差的天数
     *
     * @param smdate 较小的时间
     * @param bdate  较大的时间
     * @return 相差天数
     * @throws ParseException
     */
    public static int daysBetween(Date smdate, Date bdate) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        smdate = sdf.parse(sdf.format(smdate));
        bdate = sdf.parse(sdf.format(bdate));
        Calendar cal = Calendar.getInstance();
        cal.setTime(smdate);
        long time1 = cal.getTimeInMillis();
        cal.setTime(bdate);
        long time2 = cal.getTimeInMillis();
        long between_days = (time2 - time1) / (1000 * 3600 * 24);

        return Integer.parseInt(String.valueOf(between_days));
    }
}
